<?php

include("_mysql.php");
include("_settings.php");

function download($file, $extern = 0) {

	if(!$extern) {
		$filename = basename($file);

		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Content-Type: application/force-download");
		header("Content-Description: File Transfer");

		header("Content-Disposition: attachment; filename=".str_replace(' ', '_', $filename).";");
		header("Content-Length: ".filesize($file));
		header("Content-Transfer-Encoding: binary");

		@readfile($file);
		exit;
	}
	else header("Location: ".$file);
}

if(isset($_GET['fileID'])) $fileID = $_GET['fileID'];
if(isset($_GET['demoID'])) $demoID = $_GET['demoID'];

systeminc('session');
systeminc('login');

systeminc('func/useraccess');

if(isset($fileID)) {
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."files WHERE fileID='$fileID' ");
	$dd=mysql_fetch_array($ergebnis);

	switch($dd['accesslevel']) {
		case 0: $allowed = 1; break;
		case 1: if($userID) $allowed = 1; break;
		case 2: if(isclanmember($userID)) $allowed = 1; break;
		default: $allowed=0;
	}

	if($allowed) {

		safe_query("UPDATE ".PREFIX."files SET downloads=downloads+1 WHERE fileID='$fileID' ");

		if(stristr($dd['file'],'http://') OR stristr($dd['file'],'ftp://')) download($dd['file'], 1);
		else download('downloads/'.$dd['file']);
	}
}
elseif(isset($demoID)) {
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."demos WHERE demoID='".$demoID."'");
	$dd=mysql_fetch_array($ergebnis);

	switch($dd['accesslevel']) {
		case 0: $allowed = 1; break;
		case 1: if($userID) $allowed = 1; break;
		case 2: if(isclanmember($userID)) $allowed = 1; break;
		default: $allowed=0;
	}

	if($allowed) {

		safe_query("UPDATE ".PREFIX."demos SET downloads=downloads+1 WHERE demoID='".$demoID."'");

		if(stristr($dd['file'],'http://')) download($dd['file'],1);
		else download('demos/'.$dd['file']);

	}

}
?>
